package com.lenovo.manufacture.Fragment;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.lenovo.manufacture.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;

import static com.tencent.sonic.sdk.SonicSession.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class zidon extends Fragment {

    private Myadpter myadpter=new Myadpter();
    @BindView(R.id.zidon_gridview)
    GridView zidonGridview;
    private Gson gson = new Gson();
    private List<zidonshujv> list = new ArrayList<>();
    private int u=0;
    public zidon() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_zidon, container, false);
        ButterKnife.bind(this, inflate);
        inst();
        return inflate;
    }

    public interface Bolg {
        @GET("getProduceInfo")
        Call<ResponseBody> getBolg();
        @POST("orderWorker")Call<ResponseBody>getBolg2(@Body RequestBody body);
    }
    public void inst2(int position, String str){
        Log.d("TAG", "inst2: "+"niubi");
        Retrofit retrofit=new Retrofit.Builder().baseUrl("http://hh1.me:5001/").build();
        Bolg bolg=retrofit.create(Bolg.class);
        MediaType mediaType= MediaType.parse("application/json");
        Map<String,String>map=new HashMap<>();
        map.put("pos",position+"");
        map.put("gangwei",str);
        String s = gson.toJson(map);
        RequestBody requestBod=RequestBody.create(mediaType, s);
        Call<ResponseBody>call=bolg.getBolg2(requestBod);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Log.d(TAG, "onResponse: "+response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d("TAG", "onFailure: "+"失败了");
            }
        });
    }
    public void inst() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://hh1.me:5001/").build();
        Bolg bolg = retrofit.create(Bolg.class);
        Call<ResponseBody> call = bolg.getBolg();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    zidonshujv zidonshujv = gson.fromJson(response.body().string(), zidonshujv.class);
                    list.clear();
                    list.add(zidonshujv);
                    if (u==0){
                        zidonGridview.setAdapter(myadpter);
                    }else {
                        myadpter.notifyDataSetInvalidated();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    public class Myadpter extends BaseAdapter {
        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = View.inflate(getContext(), R.layout.zidon_grid_list, null);
                ViewHolder viewHolder=new ViewHolder(inflate);
                if (list.get(0).getData().get(position).getGcs()==null){
                    viewHolder.zidonGridListLL1.setBackgroundResource(R.color.colorAccent);
                    viewHolder.zidonGridListLL11.setVisibility(View.INVISIBLE);
                    viewHolder.zidonGridListLL12.setVisibility(View.INVISIBLE);
                    viewHolder.zidonGridListLL1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.d("TAG", "onClick: "+"被点击了");
                            inst2(position,"gcs");
                            u=1;
                            inst();
                            u=0;
                        }
                    });
                }else {
                    viewHolder.zidonGridListLL1.setBackgroundResource(R.color.colorPrimary);
                    viewHolder.zidonGridListLL11.setVisibility(View.VISIBLE);
                    viewHolder.zidonGridListLL12.setVisibility(View.VISIBLE);
                    viewHolder.zidonGridListLL11Tv1.setText(list.get(0).getData().get(position).getGcs().getName()+"");
                    viewHolder.zidonGridListLL12Tv2.setText(list.get(0).getData().get(position).getGcs().getPower()+"");
                }

                if (list.get(0).getData().get(position).getJsy()==null){
                    viewHolder.zidonGridListLL2.setBackgroundResource(R.color.colorAccent);
                    viewHolder.zidonGridListLL21.setVisibility(View.INVISIBLE);
                    viewHolder.zidonGridListLL22.setVisibility(View.INVISIBLE);
                        viewHolder.zidonGridListLL2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                inst2(position,"jsy");
                                u=1;
                                inst();
                                u=0;
                            }
                        });
                }else {
                        viewHolder.zidonGridListLL2.setBackgroundResource(R.color.colorPrimary);
                        viewHolder.zidonGridListLL21.setVisibility(View.VISIBLE);
                        viewHolder.zidonGridListLL22.setVisibility(View.VISIBLE);
                        viewHolder.zidonGridListLL21Tv1.setText(list.get(0).getData().get(position).getJsy().getName()+"");
                        viewHolder.zidonGridListLL22Tv2.setText(list.get(0).getData().get(position).getJsy().getPower()+"");
                    }
                if (list.get(0).getData().get(position).getCzy()==null){
                    viewHolder.zidonGridListLL3.setBackgroundResource(R.color.colorAccent);
                    viewHolder.zidonGridListLL31.setVisibility(View.INVISIBLE);
                    viewHolder.zidonGridListLL32.setVisibility(View.INVISIBLE);
                        viewHolder.zidonGridListLL3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                inst2(position,"czy");
                                u=1;
                                inst();
                                u=0;
                            }
                        });
                }else {
                        viewHolder.zidonGridListLL3.setBackgroundResource(R.color.colorPrimary);
                        viewHolder.zidonGridListLL31.setVisibility(View.VISIBLE);
                        viewHolder.zidonGridListLL32.setVisibility(View.VISIBLE);
                        viewHolder.zidonGridListLL31Tv1.setText(list.get(0).getData().get(position).getCzy().getName()+"");
                        viewHolder.zidonGridListLL32Tv2.setText(list.get(0).getData().get(position).getCzy().getPower()+"");
                    }
                    if(list.get(0).getData().get(position).getZjy()==null){
                    viewHolder.zidonGridListLL4.setBackgroundResource(R.color.colorAccent);
                    viewHolder.zidonGridListLL41.setVisibility(View.INVISIBLE);
                    viewHolder.zidonGridListLL42.setVisibility(View.INVISIBLE);
                        viewHolder.zidonGridListLL4.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                inst2(position,"zjy");
                                u=1;
                                inst();
                                u=0;
                            }
                        });
                }else {
                    viewHolder.zidonGridListLL4.setBackgroundResource(R.color.colorPrimary);
                    viewHolder.zidonGridListLL41.setVisibility(View.VISIBLE);
                    viewHolder.zidonGridListLL42.setVisibility(View.VISIBLE);
                    viewHolder.zidonGridListLL41Tv1.setText(list.get(0).getData().get(position).getZjy().getName()+"");
                    viewHolder.zidonGridListLL42Tv2.setText(list.get(0).getData().get(position).getZjy().getPower()+"");
                }
            return inflate;
        }

        class ViewHolder {
            @BindView(R.id.zidon_grid_list_LL1_1_tv1)
            TextView zidonGridListLL11Tv1;
            @BindView(R.id.zidon_grid_list_LL1_1)
            LinearLayout zidonGridListLL11;
            @BindView(R.id.zidon_grid_list_LL1_2_tv2)
            TextView zidonGridListLL12Tv2;
            @BindView(R.id.zidon_grid_list_LL1_2)
            LinearLayout zidonGridListLL12;
            @BindView(R.id.zidon_grid_list_LL1)
            LinearLayout zidonGridListLL1;
            @BindView(R.id.zidon_grid_list_LL2_1_tv1)
            TextView zidonGridListLL21Tv1;
            @BindView(R.id.zidon_grid_list_LL2_1)
            LinearLayout zidonGridListLL21;
            @BindView(R.id.zidon_grid_list_LL2_2_tv2)
            TextView zidonGridListLL22Tv2;
            @BindView(R.id.zidon_grid_list_LL2_2)
            LinearLayout zidonGridListLL22;
            @BindView(R.id.zidon_grid_list_LL2)
            LinearLayout zidonGridListLL2;
            @BindView(R.id.zidon_grid_list_LL3_1_tv1)
            TextView zidonGridListLL31Tv1;
            @BindView(R.id.zidon_grid_list_LL3_1)
            LinearLayout zidonGridListLL31;
            @BindView(R.id.zidon_grid_list_LL3_2_tv2)
            TextView zidonGridListLL32Tv2;
            @BindView(R.id.zidon_grid_list_LL3_2)
            LinearLayout zidonGridListLL32;
            @BindView(R.id.zidon_grid_list_LL3)
            LinearLayout zidonGridListLL3;
            @BindView(R.id.zidon_grid_list_LL4_1_tv1)
            TextView zidonGridListLL41Tv1;
            @BindView(R.id.zidon_grid_list_LL4_1)
            LinearLayout zidonGridListLL41;
            @BindView(R.id.zidon_grid_list_LL4_2_tv2)
            TextView zidonGridListLL42Tv2;
            @BindView(R.id.zidon_grid_list_LL4_2)
            LinearLayout zidonGridListLL42;
            @BindView(R.id.zidon_grid_list_LL4)
            LinearLayout zidonGridListLL4;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
